/*
	Twitch
	Please leave credits to the origional devs where its due if edited.
*/

function GM:InitPostEntity()
end

function GM:Think()
	for _,ply in pairs(player.GetAll()) do // I replaced the GM:PlayerDeathThink() since it wont be called (Players are always dead, it is the ship that dies).
		if ply.RespawnTime and ply.RespawnTime <= CurTime() then
			DebugPrint("Repawning player \""..tostring(ply).."\"!")
			ply:Spawn()
			ply.RespawnTime = nil
		end
	end
end

function GM:ShowHelp(ply)
	ply:ConCommand("twc_openhelp")
end

function GM:ShowTeam(ply)
	ply:ConCommand("twc_WarpMenu")
end

function GM:PlayerInitialSpawn(ply)
	local t = "spectator"
	if table.Count(team.GetPlayers(1)) > table.Count(team.GetPlayers(1)) then
		ply:SetTeam(2)
		t = "blue"
	else
		ply:SetTeam(1)
		t = "red"
	end
	self:LoadPlayerData(ply)
	ply:ChatPrint("Welcome to Twitch - You are on the "..t.." team.")
end

function GM:PlayerSpawn(ply)
	if ply.Ship and ply.Ship:IsValid() then
		ply.Ship:Remove()
	end
	local Ship = ents.Create("twc_ship")
	Ship:SetPos(ply:GetPos())
	Ship:Spawn()
	Ship:Activate()
	Ship:SetPlayer(ply)
	ply:SetShip(Ship)
end

function GM:PlayerSelectSpawn(ply)
	return table.Random(self:GetSpawnsFromTeam(ply:Team()))
end

function GM:GetSpawnsFromTeam(t)
	local RedSpawns = ents.FindByClass("twc_redspawn")
	local BlueSpawns = ents.FindByClass("twc_bluespawn")
	local DefaultSpawn = ents.FindByClass("info_player_start")
	if table.Count(RedSpawns) <= 0 or table.Count(BlueSpawns) <= 0 then
		return DefaultSpawn
	else
		return t == 1 and RedSpawns or t == 2 and BlueSpawns or DefaultSpawn
	end
end

function GM:PlayerAuthed(ply,SteamID,UniqueID)
end

function GM:PlayerDisconnected(ply)
	self:SavePlayerData(ply)
	if ply:GetShip() and ply:GetShip():IsValid() then
		ply:GetShip():Remove()
	end
end

function GM:PlayerDeath(ply,wep,killer)
	DebugPrint("Player \""..tostring(ply).."\" died, killed by \""..tostring(killer).."\" with weapon \""..tostring(wep).."\"")
	if killer:IsPlayer() then
		killer:SetMoney(killer:GetMoney()+50)
	end
	ply.RespawnTime = CurTime()+10
end

function GM:PlayerDeathThink(ply) // We replaced this func in the think.
end

function GM:PlayerSpray(ply)
	return false
end

function GM:CanPlayerSuicide(ply)
	return ply:IsAdmin() or SinglePlayer()
end

function GM:PlayerSwitchFlashlight(ply,SwitchOn)
	return !SwitchOn // Only turn off
end

function GM:LoadPlayerData(ply)
	if self.UseSQL then
		if self.SQLType == "SQLLite" then
			
		elseif self.SQLType == "MySQL" then
			if type(self.MySQL.LoadPlayer) == "function" then
				self.MySQL.LoadPlayer(ply)
			else // Stop using the loading if the function failed but don't make the gamemode break.
				GM.LoadPlayerData = function(self,ply) end
			end
		end
	end
end

function GM:SavePlayerData(ply)
	if self.UseSQL then
		if self.SQLType == "SQLLite" then
			
		elseif self.SQLType == "MySQL" then
			if type(self.MySQL.SavePlayer) == "function" then
				self.MySQL.SavePlayer(ply)
			else // Stop using the loading if the function failed but don't make the gamemode break.
				GM.SavePlayerData = function(self,ply) end
			end
		end
	end
end
